package com.tiger.life;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ResultActivity extends BaseActivity {
	private Bundle bundle;
	private int checkFlag;
	
	// 公共表单
	private ImageButton other, recheck;
	private LinearLayout over;
	// cigar
	private TextView age_years, age_days, bad_days, bad_hours;
	// baby
	private TextView babyResult;
	// weight
	private TextView weight_result_txt;
	private float resultFloat = 0;
	// 卡路里
	private TextView calorieResult;
	private TextView calorieTitle;
	private TextView clickTxt;
	private ImageView resImg;
	// 魔法衣柜
	private TextView chest_result, shihe, bushihe;

	// 星座情缘
	private TextView resultTitle,resultText;
	private ImageView resultImg;
	// 生活达人
	// 谣言粉碎机（略）
	// 狗狗聪明测试
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		bundle = this.getIntent().getExtras();
		checkFlag = bundle.getInt(CommonUtil.checkFlag);

		if (checkFlag == CommonUtil.checkFlagValue.cigar_Flag) {
			setContentView(R.layout.cigar_result);
		} else if (checkFlag == CommonUtil.checkFlagValue.baby_Flag) {
			setContentView(R.layout.baby_result);
		} else if (checkFlag == CommonUtil.checkFlagValue.weight_Flag) {
			setContentView(R.layout.weight_result);
		} else if (checkFlag == CommonUtil.checkFlagValue.calorie_Flag) {
			setContentView(R.layout.calorie_result);
		} else if (checkFlag == CommonUtil.checkFlagValue.chest_Flag) {
			setContentView(R.layout.chest_result);
		} else if (checkFlag == CommonUtil.checkFlagValue.xingzuo_Flag) {
			setContentView(R.layout.xingzuo_result);
		} else if (checkFlag == CommonUtil.checkFlagValue.eredar_Flag) {
			setContentView(R.layout.eredar_result);
		} else if (checkFlag == CommonUtil.checkFlagValue.rumor_Flag) {
			setContentView(R.layout.rumar_result);
		} else {
			setContentView(R.layout.dog_result);
		}
		initView();
		initAction();
	}

	public void initView() {
		other = (ImageButton) this.findViewById(R.id.other);
		recheck = (ImageButton) this.findViewById(R.id.recheck);
		if (checkFlag == CommonUtil.checkFlagValue.cigar_Flag) {
			// cigar
			over=(LinearLayout)this.findViewById(R.id.lookTo);
			age_years = (TextView) this.findViewById(R.id.age_years);
			age_days = (TextView) this.findViewById(R.id.age_days);
			bad_days = (TextView) this.findViewById(R.id.bad_days);
			bad_hours = (TextView) this.findViewById(R.id.bad_hours);
			int age_years_int = bundle.getInt(CommonUtil.age_years);
			int age_days_int = bundle.getInt(CommonUtil.age_days);
			int bad_days_int = bundle.getInt(CommonUtil.bad_days);
			int bad_hours_int = bundle.getInt(CommonUtil.bad_hours);
			age_years.setText("" + age_years_int);
			age_days.setText("" + age_days_int);
			bad_days.setText("" + bad_days_int);
			bad_hours.setText("" + bad_hours_int);
		} else if (checkFlag == CommonUtil.checkFlagValue.baby_Flag) {
			over=(LinearLayout)this.findViewById(R.id.lookTo);
			babyResult = (TextView) this.findViewById(R.id.baby_check_q_title);
			// weight
			String ret = bundle.getString(CommonUtil.babyResult);
			if ("A".equals(ret)) {
				babyResult.setText(this.getString(R.string.baby_answer_1));
			} else if ("B".equals(ret)) {
				babyResult.setText(this.getString(R.string.baby_answer_2));
			} else if ("C".equals(ret)) {
				babyResult.setText(this.getString(R.string.baby_answer_3));
			} else {
				babyResult.setText(this.getString(R.string.baby_answer_4));
			}
		} else if (checkFlag == CommonUtil.checkFlagValue.weight_Flag) {
			over=(LinearLayout)this.findViewById(R.id.lookTo);
			weight_result_txt = (TextView) this
					.findViewById(R.id.weight_result_txt);
			// weight
			resultFloat = bundle.getFloat(CommonUtil.weightResult);
			if (resultFloat <= 5.0) {
				weight_result_txt.setText(this
						.getString(R.string.answer_title_1)
						+ "\n\n" + this.getString(R.string.answer_1));
			} else if (resultFloat > 5.0 && resultFloat <= 10.0) {
				weight_result_txt.setText(this
						.getString(R.string.answer_title_2)
						+ "\n\n" + this.getString(R.string.answer_2));
			} else {
				weight_result_txt.setText(this
						.getString(R.string.answer_title_3)
						+ "\n\n" + this.getString(R.string.answer_3));
			}
		} else if (checkFlag == CommonUtil.checkFlagValue.calorie_Flag) {
			calorieResult = (TextView) this.findViewById(R.id.calorieResult);
			calorieTitle=(TextView)this.findViewById(R.id.calorieTitle);
			clickTxt=(TextView)this.findViewById(R.id.clickTxt);
			resImg=(ImageView)this.findViewById(R.id.resImg);
			float result = bundle.getFloat(CommonUtil.calorieInter.result);
			float biaozhun = bundle.getFloat(CommonUtil.calorieInter.biaozhun);
			float zongsheru = bundle.getFloat(CommonUtil.calorieInter.zongsheru);
			String resultDesc="您本餐摄入卡路里的量未能达标，还差（-"+(int)result+"）卡，再吃点呗……";
			String yundongStr = "";
			
			String[] yundongs = null;
			if (result < 0) {
				resImg.setImageResource(R.drawable.keai);
				yundongs = null;
			}else if(result==0){
				yundongs=null;
				resImg.setImageResource(R.drawable.daxiao);
				resultDesc="祝贺您！本餐摄入卡路里的量刚刚好哟！请继续坚持！";
			}else if (result > 0 && result <= 100) {
				yundongs = this.getResources()
						.getStringArray(R.array.level_100);
			} else if (result > 100 && result <= 200) {
				yundongs = this.getResources()
						.getStringArray(R.array.level_200);
			} else if (result > 200 && result <= 300) {
				yundongs = this.getResources()
						.getStringArray(R.array.level_300);
			} else if (result > 300 && result <= 400) {
				yundongs = this.getResources()
						.getStringArray(R.array.level_400);
			} else if (result > 400 && result <= 500) {
				yundongs = this.getResources()
						.getStringArray(R.array.level_500);
			} else if (result > 500 && result <= 600) {
				yundongs = this.getResources()
						.getStringArray(R.array.level_600);
			} else if (result > 600 && result <= 700) {
				yundongs = this.getResources()
						.getStringArray(R.array.level_700);
			} else {
				yundongs = this.getResources().getStringArray(
						R.array.level_700up);
			}
			if (yundongs != null) {
				resImg.setImageResource(R.drawable.weixiao);
				resultDesc="您超标"+(int)result+"，微笑"+(int)(result/50)+"分钟可消耗，以4公里/小时时速度"+(int)(result/255)+"分钟可消耗";
//				resultDesc="您本餐摄入卡路里"+zongsheru+"卡，超标"+result+"卡哟！我们向您推荐消耗超标卡路里的运动方式如下:";
				yundongStr = "";
				for (int i=0;i<yundongs.length;i++) {
					String arr[]=yundongs[i].split("\\|");
					yundongStr += arr[1]+"(消耗"+arr[2]+"卡)";
					if(i<yundongs.length-1){
						yundongStr+="\n";
					}
				}
			}
			
			calorieTitle.setText(resultDesc);
			clickTxt.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					calorieResult.setVisibility(View.VISIBLE);
				}
			});
			calorieResult.setText(yundongStr);
			calorieResult.setVisibility(View.GONE);
		} else if (checkFlag == CommonUtil.checkFlagValue.chest_Flag) {
			over=(LinearLayout)this.findViewById(R.id.lookTo);
			String colorSel = bundle.getString(CommonUtil.chestInter.colorSel);
			chest_result = (TextView) this
					.findViewById(R.id.baby_check_q_title);
			shihe = (TextView) this.findViewById(R.id.shihe_desc);
			bushihe = (TextView) this.findViewById(R.id.bushihe_desc);
			chest_result.setText(Html.fromHtml(""
					+ this.getResources().getString(
							this.getResources().getIdentifier(
									"com.tiger.life:string/desc_" + colorSel,
									null, null))));
			shihe.setText(Html.fromHtml("<font color='red'>适合的服饰色彩：</font>"
					+ this.getResources().getString(
							this.getResources().getIdentifier(
									"com.tiger.life:string/yes_" + colorSel,
									null, null))));
			bushihe.setText(Html.fromHtml("<font color='red'>不适合的服饰色彩：</font>"
					+ this.getResources().getString(
							this.getResources().getIdentifier(
									"com.tiger.life:string/no_" + colorSel,
									null, null))));
		} else if (checkFlag == CommonUtil.checkFlagValue.xingzuo_Flag) {
			resultTitle=(TextView)this.findViewById(R.id.resultTitle);
			resultImg=(ImageView)this.findViewById(R.id.resultImg);
			resultText=(TextView)this.findViewById(R.id.resultText);
			int index=bundle.getInt(CommonUtil.xingzuoInter.index);
			String title=bundle.getString(CommonUtil.xingzuoInter.title);
			resultTitle.setText(title);
			resultImg.setBackgroundResource(this.getResources().getIdentifier(
					"com.tiger.life:drawable/xingzuo_"+index,
					null, null));
			resultText.setText(CommonUtil.getTextByTxt(this, "xingzuo_"+index+".txt"));
		} else if (checkFlag == CommonUtil.checkFlagValue.eredar_Flag) {

		} else if (checkFlag == CommonUtil.checkFlagValue.rumor_Flag) {
			resultTitle=(TextView)this.findViewById(R.id.resultTitle);
			resultText=(TextView)this.findViewById(R.id.resultText);
			int index=bundle.getInt(CommonUtil.xingzuoInter.index);
			String title=bundle.getString(CommonUtil.xingzuoInter.title);
			resultTitle.setText(title);
			resultText.setText(CommonUtil.getTextByTxt(this, "rumar_"+index+".txt"));
		} else {
			weight_result_txt = (TextView) this
					.findViewById(R.id.baby_check_q_title);
			resultFloat = bundle.getFloat(CommonUtil.dogResult);
			if (resultFloat <= 14) {
				weight_result_txt.setText(R.string.dog_answer_1);
			} else if (resultFloat >= 15 && resultFloat <= 18) {
				weight_result_txt.setText(R.string.dog_answer_2);
			} else if (resultFloat >= 19 && resultFloat <= 23) {
				weight_result_txt.setText(R.string.dog_answer_3);
			} else if (resultFloat >= 24 && resultFloat <= 27) {
				weight_result_txt.setText(R.string.dog_answer_4);
			} else if (resultFloat >= 28 && resultFloat <= 31) {
				weight_result_txt.setText(R.string.dog_answer_5);
			} else if (resultFloat >= 32 && resultFloat <= 37) {
				weight_result_txt.setText(R.string.dog_answer_6);
			} else if (resultFloat >= 38 && resultFloat <= 41) {
				weight_result_txt.setText(R.string.dog_answer_7);
			} else {
				weight_result_txt.setText(R.string.dog_answer_8);
			}
		}
	}
	
	
	OnClickListener overListner=new OnClickListener(){
		@Override
		public void onClick(View v) {
			Intent overIntent=new Intent(ResultActivity.this,OverActivity.class);
			overIntent.putExtras(bundle);
			ResultActivity.this.startActivity(overIntent);
			// TODO Auto-generated method stub
		}
	};
	public void initAction() {
		if (checkFlag == CommonUtil.checkFlagValue.cigar_Flag) {
    		over.setOnClickListener(overListner);
    	} else if (checkFlag == CommonUtil.checkFlagValue.baby_Flag) {
    		over.setOnClickListener(overListner);
    	} else if (checkFlag == CommonUtil.checkFlagValue.weight_Flag) {
    		over.setOnClickListener(overListner);
    	} else if (checkFlag == CommonUtil.checkFlagValue.calorie_Flag) {

    	} else if (checkFlag == CommonUtil.checkFlagValue.chest_Flag) {
    		over.setOnClickListener(overListner);
    	} else if (checkFlag == CommonUtil.checkFlagValue.xingzuo_Flag) {

		} else if (checkFlag == CommonUtil.checkFlagValue.eredar_Flag) {

		} else if (checkFlag == CommonUtil.checkFlagValue.rumor_Flag) {

		} else {
			
		}
		if (checkFlag != CommonUtil.checkFlagValue.xingzuo_Flag&&
				checkFlag != CommonUtil.checkFlagValue.rumor_Flag) {
		other.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 其他跳到主页
				Intent intent = new Intent(ResultActivity.this,
						IndexActivity.class);
				ResultActivity.this.startActivity(intent);
			}

		});
		recheck.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 重新测试跳转到当前测试的start页
				Intent intent = new Intent(ResultActivity.this,
						StartActivity.class);
				intent.putExtras(bundle);
				ResultActivity.this.startActivity(intent);
			}

		});
		}
	}
}